﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TheBox
{
    class Particle
    {
        public static int Height = 10;
        public static int Width = 10;
        public Vector2 Speed = new Vector2(0,0);
        public Vector2 Pos = new Vector2();
        public Box BaseBox;
        public Texture2D Texture;
        public Rectangle Rect;
        private Random Rand;

        public Particle(Box baseBox, Texture2D texture,Random Rand)
        {
            this.Rand = Rand;
            BaseBox = baseBox;
            switch(BaseBox.Quater)
            {
                case 0:
                    Pos = new Vector2(Rand.Next(Box.ParticleLeftMost, Box.ParticleRightMost), Box.ParticleUpMost);
                    break;
                case 3:
                    Pos = new Vector2(Box.ParticleRightMost, Rand.Next(Box.ParticleUpMost, Box.ParticleDownMost));
                    break;
                case 2:
                    Pos = new Vector2(Rand.Next(Box.ParticleLeftMost, Box.ParticleRightMost), Box.ParticleDownMost);
                    break;
                case 1:
                    Pos = new Vector2(Box.ParticleLeftMost, Rand.Next(Box.ParticleUpMost, Box.ParticleDownMost));
                    break;
                                
            }
            Texture = texture;
            Matrix rotationMatrix = Matrix.CreateRotationZ(-1 * BaseBox.Rotation);
            Speed += Vector2.Transform(BaseBox.Gravity, rotationMatrix) * 4;
        }

        public void update(GameTime gameTime)
        {
            Matrix rotationMatrix = Matrix.CreateRotationZ(-1 * BaseBox.Rotation);
            Speed += Vector2.Transform(BaseBox.Gravity, rotationMatrix) * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Pos += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            rotationMatrix = Matrix.CreateRotationZ(BaseBox.Rotation);
            Vector2 newPos = Vector2.Transform(Pos - Box.Origin, rotationMatrix) + BaseBox.Position;
            Rect = new Rectangle((int)newPos.X - Width / 2, (int)newPos.Y - Height / 2, Width, Height);

        }

    }
}
